/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.bradipao.deschartsdemo.fraction;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.global.configuration.Configuration;

import it.bradipao.deschartsdemo.spinner.MySpinner;
import it.bradipao.deschartsdemo.ResourceTable;
import it.bradipao.deschartsdemo.spinner.MaterialSpinner;
import it.bradipao.deschartsdemo.spinner.util.Utils;
import it.bradipao.deschartsdemo.view.MyToggleButton;
import it.bradipao.lib.descharts.ChartValue;
import it.bradipao.lib.descharts.ChartValueSerie;
import it.bradipao.lib.descharts.StackedLineChartView;

/**
 * StackedLineChartFraction
 *
 * @since 2021-07-27
 */
public class StackedLineChartFraction extends Fraction {
    private Component rootView;
    private StackedLineChartView vChart;
    private MyToggleButton tbS1, tbS2, tbS3, tbBorder, tbAxis, tbGrid, tbXtext, tbYtext, tbXtextBottom, tbYtextLeft;
    private MaterialSpinner spS1color, spS2color, spS3color, spS1size, spS2size, spS3size, spS1fillc, spS2fillc, spS3fillc;
    private MaterialSpinner spBorderC, spAxisC, spGridC, spBorderW, spAxisW, spGridW, spTextC, spTextS, spBkgC;
    private Checkbox cbUseDip, cbUseAA;
    private String[] colors = {"BLACK", "RED", "GREEN", "BLUE", "VIOLET", "YELLOW", "WHITE", "lt RED", "lt GREEN", "lt BLUE", "lt VIOLET", "lt YELLOW"};
    private int[] icolor = {
        Color.getIntColor("#ff000000"),
        Color.getIntColor("#ffcc0000"),
        Color.getIntColor("#ff669900"),
        Color.getIntColor("#ff0099cc"),
        Color.getIntColor("#ff9933cc"),
        Color.getIntColor("#ffff8800"),
        Color.getIntColor("#ffffffff"),
        Color.getIntColor("#ffff4444"),
        Color.getIntColor("#ff99cc00"),
        Color.getIntColor("#ff33bbee"),
        Color.getIntColor("#ffaa66cc"),
        Color.getIntColor("#ffffbb33")};
    private String[] widths = {"0.5", "1.0", "1.5", "2.0", "2.5", "3.0"};
    private float[] fwidth = {0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f};
    private String[] sizes = {"6.0", "6.5", "7.0", "7.5", "8.0", "9.0", "10.0"};
    private float[] fsizes = {6.0f, 6.5f, 7.0f, 7.5f, 8.0f, 9.0f, 10.0f};
    private DirectionalLayout dl_tb, dl_tb2, dl_tb3, dl_spTextSize;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        rootView = scatter.parse(ResourceTable.Layout_fr_stackedlinechart, container, false);
        getViews();
        Configuration configuration = getFractionAbility().getResourceManager().getConfiguration();
        updateDirection(configuration);
        ChartValueSerie rr = new ChartValueSerie(Color.RED.getValue(), 1);
        rr.addPoint(new ChartValue("jan", 10));
        rr.addPoint(new ChartValue("feb", 15));
        rr.addPoint(new ChartValue("mar", 25));
        rr.addPoint(new ChartValue("apr", 30));
        rr.addPoint(new ChartValue("may", 15));
        rr.addPoint(new ChartValue("jun", 30));
        rr.addPoint(new ChartValue("jul", 70));
        rr.addPoint(new ChartValue("aug", 100));
        rr.addPoint(new ChartValue("sep", 130));
        rr.addPoint(new ChartValue("oct", 125));
        rr.addPoint(new ChartValue("nov", 120));
        rr.addPoint(new ChartValue("dec", 115));

        ChartValueSerie gg = new ChartValueSerie(Color.GREEN.getValue(), 2);
        gg.addPoint(new ChartValue("jan", 15));
        gg.addPoint(new ChartValue("feb", 30));
        gg.addPoint(new ChartValue("mar", 50));
        gg.addPoint(new ChartValue("apr", 75));
        gg.addPoint(new ChartValue("may", 100));
        gg.addPoint(new ChartValue("jun", 70));
        gg.addPoint(new ChartValue("jul", 60));
        gg.addPoint(new ChartValue("aug", 45));
        gg.addPoint(new ChartValue("sep", 20));
        gg.addPoint(new ChartValue("oct", 15));
        gg.addPoint(new ChartValue("nov", 10));
        gg.addPoint(new ChartValue("dec", 5));

        ChartValueSerie bb = new ChartValueSerie(Color.BLUE.getValue(), 3);
        bb.addPoint(new ChartValue("jan", 150));
        bb.addPoint(new ChartValue("feb", 120));
        bb.addPoint(new ChartValue("mar", 100));
        bb.addPoint(new ChartValue("apr", 90));
        bb.addPoint(new ChartValue("may", 80));
        bb.addPoint(new ChartValue("jun", 70));
        bb.addPoint(new ChartValue("jul", 55));
        bb.addPoint(new ChartValue("aug", 40));
        bb.addPoint(new ChartValue("sep", 25));
        bb.addPoint(new ChartValue("oct", 35));
        bb.addPoint(new ChartValue("nov", 40));
        bb.addPoint(new ChartValue("dec", 50));

        vChart.setLabelMaxNum(12);
        vChart.addSerie(rr);
        vChart.addSerie(gg);
        vChart.addSerie(bb);
        setupListeners();
        setupViews();

        return rootView;
    }

    private void getViews() {
        vChart = (StackedLineChartView) rootView.findComponentById(ResourceTable.Id_chart);
        tbS1 = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbSerie1);
        spS1color = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie1Color);
        spS1size = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie1Size);
        tbS2 = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbSerie2);
        spS2color = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie2Color);
        spS2size = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie2Size);
        tbS3 = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbSerie3);
        spS3color = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie3Color);
        spS3size = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie3Size);
        spS1fillc = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie1FillColor);
        spS2fillc = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie2FillColor);
        spS3fillc = (MySpinner) rootView.findComponentById(ResourceTable.Id_spSerie3FillColor);
        tbBorder = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbBorder);
        tbAxis = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbAxis);
        tbGrid = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbGrid);
        spBorderC = (MySpinner) rootView.findComponentById(ResourceTable.Id_spBorderC);
        spAxisC = (MySpinner) rootView.findComponentById(ResourceTable.Id_spAxisC);
        spGridC = (MySpinner) rootView.findComponentById(ResourceTable.Id_spGridC);
        spBorderW = (MySpinner) rootView.findComponentById(ResourceTable.Id_spBorderW);
        spAxisW = (MySpinner) rootView.findComponentById(ResourceTable.Id_spAxisW);
        spGridW = (MySpinner) rootView.findComponentById(ResourceTable.Id_spGridW);
        cbUseDip = (Checkbox) rootView.findComponentById(ResourceTable.Id_cbUseDip);
        cbUseAA = (Checkbox) rootView.findComponentById(ResourceTable.Id_cbUseAA);
        tbXtext = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbXtext);
        tbYtext = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbYtext);
        tbXtextBottom = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbXtextBottom);
        tbYtextLeft = (MyToggleButton) rootView.findComponentById(ResourceTable.Id_tbYtextLeft);
        spTextC = (MySpinner) rootView.findComponentById(ResourceTable.Id_spTextColor);
        spTextS = (MySpinner) rootView.findComponentById(ResourceTable.Id_spTextSize);
        spBkgC = (MySpinner) rootView.findComponentById(ResourceTable.Id_spBkgColor);
        dl_tb = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_dl_tb);
        dl_tb2 = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_dl_tb2);
        dl_tb3 = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_dl_tb3);
        dl_spTextSize = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_dl_spTextSize);
    }

    private void setupViews() {
        tbS1.setChecked(true);
        tbS2.setChecked(true);
        tbS3.setChecked(true);
        tbBorder.setChecked(true);
        tbAxis.setChecked(true);
        tbGrid.setChecked(true);
        cbUseDip.setChecked(true);
        cbUseAA.setChecked(true);
        tbXtext.setChecked(true);
        tbYtext.setChecked(true);
        tbXtextBottom.setChecked(true);
        tbYtextLeft.setChecked(true);
        spS1color.setItems(colors);
        spS2color.setItems(colors);
        spS3color.setItems(colors);
        spS1size.setItems(widths);
        spS2size.setItems(widths);
        spS3size.setItems(widths);
        spBorderC.setItems(colors);
        spAxisC.setItems(colors);
        spGridC.setItems(colors);
        spBorderW.setItems(widths);
        spAxisW.setItems(widths);
        spGridW.setItems(widths);
        spTextS.setItems(sizes);
        spTextC.setItems(colors);
        spBkgC.setItems(colors);
        spS1fillc.setItems(colors);
        spS2fillc.setItems(colors);
        spS3fillc.setItems(colors);
        spS1color.setSelectedIndex(7);
        spS2color.setSelectedIndex(8);
        spS3color.setSelectedIndex(9);
        spS1size.setSelectedIndex(1);
        spS2size.setSelectedIndex(3);
        spS3size.setSelectedIndex(5);
        spS1fillc.setSelectedIndex(1);
        spS2fillc.setSelectedIndex(2);
        spS3fillc.setSelectedIndex(3);
        spBorderC.setSelectedIndex(6);
        spAxisC.setSelectedIndex(6);
        spGridC.setSelectedIndex(6);
        spBorderW.setSelectedIndex(1);
        spAxisW.setSelectedIndex(1);
        spGridW.setSelectedIndex(1);
        spTextC.setSelectedIndex(6);
        spTextS.setSelectedIndex(5);
        spBkgC.setSelectedIndex(0);
        spBorderC.setTextSize(50);
        spAxisC.setTextSize(50);
        spGridC.setTextSize(50);
        spS1color.setTextSize(50);
        spS2color.setTextSize(50);
        spS3color.setTextSize(50);
        spS1size.setTextSize(50);
        spS2size.setTextSize(50);
        spS3size.setTextSize(50);
        spS1fillc.setTextSize(50);
        spS2fillc.setTextSize(50);
        spS3fillc.setTextSize(50);
        spBorderW.setTextSize(50);
        spAxisW.setTextSize(50);
        spGridW.setTextSize(50);
        spTextS.setTextSize(50);
        spTextS.setTextSize(50);
        spTextC.setTextSize(50);
        spBkgC.setTextSize(50);
    }

    private void setupListeners() {
        MyToggleButton.MyToggleCheckedChangedListener mSerieListener = new MyToggleButton.MyToggleCheckedChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                vChart.setLineVis(0, tbS1.isChecked());
                vChart.setLineVis(1, tbS2.isChecked());
                vChart.setLineVis(2, tbS3.isChecked());
            }
        };
        tbS1.setMyToggleCheckedChangedListener(mSerieListener);
        tbS2.setMyToggleCheckedChangedListener(mSerieListener);
        tbS3.setMyToggleCheckedChangedListener(mSerieListener);
        MyToggleButton.MyToggleCheckedChangedListener mGridListener = new MyToggleButton.MyToggleCheckedChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                vChart.setGridVis(tbBorder.isChecked(), tbGrid.isChecked(), tbAxis.isChecked());
            }
        };
        tbBorder.setMyToggleCheckedChangedListener(mGridListener);
        tbAxis.setMyToggleCheckedChangedListener(mGridListener);
        tbGrid.setMyToggleCheckedChangedListener(mGridListener);
        cbUseDip.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                if (isChecked) vChart.setGridWidthDip(fwidth[spBorderW.getSelectedIndex()],
                    fwidth[spGridW.getSelectedIndex()],
                    fwidth[spAxisW.getSelectedIndex()]);
                else vChart.setGridWidth(fwidth[spBorderW.getSelectedIndex()],
                    fwidth[spGridW.getSelectedIndex()],
                    fwidth[spAxisW.getSelectedIndex()]);
            }

        });
        cbUseAA.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                vChart.setGridAA(isChecked);
            }
        });

        MyToggleButton.MyToggleCheckedChangedListener mTextListener = new MyToggleButton.MyToggleCheckedChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                vChart.setTextVis(tbXtext.isChecked(), tbYtext.isChecked(), tbXtextBottom.isChecked(), tbYtextLeft.isChecked());
            }
        };
        tbXtext.setMyToggleCheckedChangedListener(mTextListener);
        tbYtext.setMyToggleCheckedChangedListener(mTextListener);
        tbXtextBottom.setMyToggleCheckedChangedListener(mTextListener);
        tbYtextLeft.setMyToggleCheckedChangedListener(mTextListener);
        MaterialSpinner.OnItemSelectedListener mSerieColorListener = new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner materialSpinner, int i, long l, Object o) {
                vChart.setLineStyle(0, icolor[spS1color.getSelectedIndex()], icolor[spS1fillc.getSelectedIndex()], fwidth[spS1size.getSelectedIndex()], true);
                vChart.setLineStyle(1, icolor[spS2color.getSelectedIndex()], icolor[spS2fillc.getSelectedIndex()], fwidth[spS2size.getSelectedIndex()], true);
                vChart.setLineStyle(2, icolor[spS3color.getSelectedIndex()], icolor[spS3fillc.getSelectedIndex()], fwidth[spS3size.getSelectedIndex()], true);
            }
        };
        mSerieColorListener.onItemSelected(null, 0, 0, null);
        spS1color.setOnItemSelectedListener(mSerieColorListener);
        spS2color.setOnItemSelectedListener(mSerieColorListener);
        spS3color.setOnItemSelectedListener(mSerieColorListener);
        spS1fillc.setOnItemSelectedListener(mSerieColorListener);
        spS2fillc.setOnItemSelectedListener(mSerieColorListener);
        spS3fillc.setOnItemSelectedListener(mSerieColorListener);
        spS1size.setOnItemSelectedListener(mSerieColorListener);
        spS2size.setOnItemSelectedListener(mSerieColorListener);
        spS3size.setOnItemSelectedListener(mSerieColorListener);

        MaterialSpinner.OnItemSelectedListener mGridColorListener = new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner materialSpinner, int i, long l, Object o) {
                vChart.setGridColor(icolor[spBorderC.getSelectedIndex()],
                    icolor[spGridC.getSelectedIndex()],
                    icolor[spAxisC.getSelectedIndex()]);
            }
        };
        spBorderC.setOnItemSelectedListener(mGridColorListener);
        spGridC.setOnItemSelectedListener(mGridColorListener);
        spAxisC.setOnItemSelectedListener(mGridColorListener);
        MaterialSpinner.OnItemSelectedListener mGridWidthListener = new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner materialSpinner, int i, long l, Object o) {
                if (cbUseDip.isChecked()) vChart.setGridWidthDip(fwidth[spBorderW.getSelectedIndex()],
                    fwidth[spGridW.getSelectedIndex()],
                    fwidth[spAxisW.getSelectedIndex()]);
                else vChart.setGridWidth(fwidth[spBorderW.getSelectedIndex()],
                    fwidth[spGridW.getSelectedIndex()],
                    fwidth[spAxisW.getSelectedIndex()]);
            }
        };
        spBorderW.setOnItemSelectedListener(mGridWidthListener);
        spGridW.setOnItemSelectedListener(mGridWidthListener);
        spAxisW.setOnItemSelectedListener(mGridWidthListener);
        MaterialSpinner.OnItemSelectedListener mTextStyleListener = new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner materialSpinner, int i, long l, Object o) {
                vChart.setTextStyle(icolor[spTextC.getSelectedIndex()],
                    fsizes[spTextS.getSelectedIndex()]);
            }
        };
        spTextC.setOnItemSelectedListener(mTextStyleListener);
        spTextS.setOnItemSelectedListener(mTextStyleListener);
        MaterialSpinner.OnItemSelectedListener mBkgColorListener = new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner materialSpinner, int i, long l, Object o) {
                vChart.setBackgroundColor(icolor[spBkgC.getSelectedIndex()]);
            }
        };
        spBkgC.setOnItemSelectedListener(mBkgColorListener);
    }

    private void updateDirection(Configuration configuration) {
        if (configuration.direction == Configuration.DIRECTION_HORIZONTAL) {
            dl_tb.setMarginLeft((int) Utils.convertDpToPixel(330));
            dl_tb.setMarginRight((int) Utils.convertDpToPixel(20));
            dl_tb2.setMarginLeft((int) Utils.convertDpToPixel(330));
            dl_tb2.setMarginRight((int) Utils.convertDpToPixel(20));
            dl_tb3.setMarginLeft((int) Utils.convertDpToPixel(330));
            dl_tb3.setMarginRight((int) Utils.convertDpToPixel(20));
            dl_spTextSize.setMarginLeft((int) Utils.convertDpToPixel(1020));
            dl_spTextSize.setMarginRight((int) Utils.convertDpToPixel(20));
            cbUseAA.setMarginLeft((int) Utils.convertDpToPixel(620));
        } else {
            dl_tb.setMarginLeft(180);
            dl_tb.setMarginRight(15);
            dl_tb2.setMarginLeft(180);
            dl_tb2.setMarginRight(15);
            dl_tb3.setMarginLeft(180);
            dl_tb3.setMarginRight(15);
            dl_spTextSize.setMarginLeft(520);
            dl_spTextSize.setMarginRight(15);
            cbUseAA.setMarginRight((int) Utils.convertDpToPixel(150));
        }
    }
}
